package site.yunnong.atvris.portal.service.impl;

import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import site.yunnong.atvris.common.utils.CopyUtils;
import site.yunnong.atvris.dao.domain.MediaPartition;
import site.yunnong.atvris.portal.service.PartitionService;
import site.yunnong.atvris.service.MediaPartitionService;
import site.yunnong.atvris.common.enumeration.PartitionLevelEnum;
import site.yunnong.atvris.service.vo.PartitionVO;
import site.yunnong.atvris.service.vo.PartitionWithChildrenItemVO;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zjh
 * @date 2021/8/21 12:04
 */
@Service
public class PartitionServiceImpl implements PartitionService {
    
    @Resource
    private MediaPartitionService mediaPartitionService;
    @Override
    public List<PartitionVO> queryByParentId(Long parentId) {
        List<MediaPartition> partitions = mediaPartitionService.queryAimLevelPartition(parentId, null);
        return Optional.of(partitions).orElse(Lists.newArrayList())
                .stream().map(this::buildPartitionVO).collect(Collectors.toList());
    }

    @Override
    public List<PartitionWithChildrenItemVO> listWithChildren() {
        List<MediaPartition> partitions = mediaPartitionService.queryAimLevelPartition(null, PartitionLevelEnum.FIRST_LEVEL);
        List<PartitionWithChildrenItemVO> results = CopyUtils.copyList(partitions, PartitionWithChildrenItemVO::new);
        for (PartitionWithChildrenItemVO vo : results) {
            // 获取二级分区
            List<MediaPartition> secondPartitions = mediaPartitionService.queryAimLevelPartition(vo.getId(), PartitionLevelEnum.SECOND_LEVEL);
            List<PartitionWithChildrenItemVO> secondChildrens = CopyUtils.copyList(secondPartitions, PartitionWithChildrenItemVO::new);
            for (PartitionWithChildrenItemVO secondChildren : secondChildrens) {
                // 获取三级分区
                List<MediaPartition> thirdPartitions = mediaPartitionService.queryAimLevelPartition(secondChildren.getId(), PartitionLevelEnum.THIRD_LEVEL);
                List<PartitionWithChildrenItemVO> thirdChildrens = CopyUtils.copyList(thirdPartitions, PartitionWithChildrenItemVO::new);
                secondChildren.setChildren(thirdChildrens);
            }
            vo.setChildren(secondChildrens);
        }
        return results;
    }

    @Override
    public List<PartitionVO> firstLevelPartition() {
        List<MediaPartition> partitions = mediaPartitionService.queryAimLevelPartition(null, PartitionLevelEnum.FIRST_LEVEL);
        return Optional.of(partitions).orElse(Lists.newArrayList())
                .stream().map(this::buildPartitionVO).collect(Collectors.toList());
    }

    @Override
    public List<PartitionVO> queryAll() {
        List<MediaPartition> partitionList = mediaPartitionService.queryAll();
        if (CollectionUtils.isEmpty(partitionList)) {
            return Lists.newArrayList();
        }
        return partitionList.stream().map(this::buildPartitionVO).collect(Collectors.toList());
    }

    private PartitionVO buildPartitionVO(MediaPartition partition) {
        PartitionVO vo = new PartitionVO();
        BeanUtils.copyProperties(partition, vo);
        return vo;
    }
}
